$(document).ready(function () {
    var uid = 0;
    $.getJSON('fontend.php?class=auth&action=uid', function (data) {
        uid = data.uid;
    });
    $('#btn-book-qsearch').click(function () {
        if ($('#form-bsearch').val() === '') {
            $('#form-bsearch').focus();
        } else {
            $.getJSON('fontend.php', {class: 'userlib', action: 'query', condition: $('#form-bsearch').val()}, function (data) {
                var tb = '';
                $.each(data, function (idx, it) {
                    var rent = '';
                    if (it.rent == '0') {
                        rent = '<button class="btn btn-primary btn-xs eb-rent" data-bid="' + it.bid + '" data-action="1">Rent</button>';
                    } else {
                        if (it.rent == uid) {
                            rent = '<button class="btn btn-primary btn-xs eb-return" data-bid="' + it.bid + '" data-action="2">Return</button>';
                        } else
                            rent = 'Rent Out';
                    }

                    tb += '<tr><td class="td-bid">' + it.bid + '</td><td class="td-bkname">' + it.bname + '</td><td calss="td-bkauthor">' + it.bkauthor + '</td><td class="td-bkpub">' + it.pubname + '</td><td class="td-isbn">' + it.bkisbn + '</td><td><button type="button" class="btn btn-primary btn-xs sdetail" data-bid="' + it.bid + '">Show Detail</button></td><td class="td-price">' + it.bkprice + '</td><td class="td-rent">' + rent + '</td></tr>';
                });
                if (tb === '') {
                    tb = '<tr><td colspan="8"> Not Found </td></tr>';
                }
                $('#tblist').html(tb);
            });
        }
    });

    $(document).on('click', '.sdetail', function (e) {
        var btn = $(this).data('bid');
        $.getJSON('fontend.php', {class: 'userlib', action: 'query', condition: btn}, function (data) {
            $('#dd-bookid').html(data[0].bid);
            $('#dd-bookname').html(data[0].bname);
            $('#dd-bookauthor').html(data[0].bkauthor);
            $('#dd-publisher').html(data[0].pubname);
            $('#dd-bkisbn').html(data[0].bkisbn);
            $('#dd-description').html(data[0].bkdesc);
            $('#dd-price').html(data[0].bkprice);
            if (data[0].rent == '0') {
                $('#bnrentm').text('Rent');
                $('#bnrentm').data('bid', data[0].bid);
                $('#bnrentm').data('action','1');
            } else {
                if (data[0].rent == uid) {
                    $('#bnrentm').data('action',2);
                    $('#bnrentm').text('Return');
                } else {
                    $('#bnrentm').addClass('disabled');
                }
            }
            $('#bkdetailm').modal('show');
        });

        
    });
    
    $('#bookrentm').click(function(e){
        $('#bkdetailm').modal('hide');
        $('#ddrbookid').html($('#dd-bookid').html());
        $('#ddrbookname').html($('#dd-bookname').html());
        $('#ddrbookauthor').html($('#dd-bookauthor').html());
        $('#ddrpublisher').html($('#dd-publisher').html());
        $('#ddrbkisbn').html($('#dd-bkisbn').html());
        $('#ddrdescription').html($('#dd-description').html());
        $('#ddrprice').html($('#dd-price').html());
        $("#bnrentc").data('bid',$(this).data('bid'));
        $('#bnrentc').data('action',$(this).data('action'));
        $('#bkrentm').modal('show');
    });
    
    $(document).on('click','.eb-rent,.eb-return',function(){
        var btn = $(this).data('bid');
        $('#bnrentc').data('action',$(this).data('action'));
        $.getJSON('fontend.php', {class: 'userlib', action: 'query', condition: btn}, function (data) {
            $('#ddrbookid').html(data[0].bid);
            $('#ddrbookname').html(data[0].bname);
            $('#ddrbookauthor').html(data[0].bkauthor);
            $('#ddrpublisher').html(data[0].pubname);
            $('#ddrbkisbn').html(data[0].bkisbn);
            $('#ddrdescription').html(data[0].bkdesc);
            $('#ddrprice').html(data[0].bkprice);
            if (data[0].rent == '0') {
                $('#bnrentc').text('Rent');
                $('#bnrentc').data('bid', data[0].bid);
            } else {
                if (data[0].rent == uid) {
                    $('#bnrentc').text('Return');
                } else {
                    $('#bnrentc').addClass('disabled');
                }
            }
            $('#bkrentm').modal('show');
        });
    });
    
    $('#bnrentc').click(function(){
        var act = 'rent';
        if($(this).data('action')=='2'){
            act= 'return';
        }
        $.getJSON('fontend.php',{class:'userlib',action:act,bid:$(this).data('bid')},function(data){
            if(data.errcode==0){
                $('#msgout').html('<div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> Success, You can take this book out</div>');
            }else{
                $('#msgout').html('<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> '+data.msg+'</div>');
            }
            $('bkrentm').modal('hide');
        });
    });
    
});


